#include <avr/io.h>
#define ADC_VREF_TYPE 0b01100111
void Init(void)
{
    CLKPR=0x80;
    CLKPR=0x03;

    // Input/Output Ports initialization
    // Port B initialization
    // Func7=In Func6=In Func5=Out Func4=In Func3=Out Func2=Out Func1=Out Func0=In
    // State7=P State6=P State5=0 State4=T State3=0 State2=0 State1=0 State0=P
    PORTB=0b11000000;    //0xC1;
    DDRB= 0b00101110;    //0x2E;

    // Port C initialization
    // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
    // State6=T State5=T State4=P State3=P State2=T State1=P State0=P
    PORTC=0x1B;
    DDRC=0x00;

    // Port D initialization
    // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out
    // State7=P State6=P State5=P State4=P State3=P State2=P State1=0 State0=0
    PORTD=0xFF;
    DDRD=0x00;

    // Timer/Counter 0 initialization
    // Clock source: System Clock
    // Clock value: 125.000 kHz
    // Mode: Normal top=0xFF
    // OC0A output: Disconnected
    // OC0B output: Disconnected
    TCCR0A=0x00;
    TCCR0B=0x02;
    TCNT0=0x00;
    OCR0A=0x00;
    OCR0B=0x00;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: 125.000 kHz
    // Mode: Normal top=0xFFFF
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer1 Overflow Interrupt: On
    // Input Capture Interrupt: Off
    // Compare A Match Interrupt: Off
    // Compare B Match Interrupt: Off
    TCCR1A=0x00;
    TCCR1B=0x02;
    TCNT1H=0x00;
    TCNT1L=0x00;
    ICR1H=0x00;
    ICR1L=0x00;
    OCR1AH=0x00;
    OCR1AL=0x00;
    OCR1BH=0x00;
    OCR1BL=0x00;

    // Timer/Counter 2 initialization
    // Clock source: System Clock
    // Clock value: Timer2 Stopped
    // Mode: Normal top=0xFF
    // OC2A output: Disconnected
    // OC2B output: Disconnected
    ASSR=0x00;
    TCCR2A=0x00;
    TCCR2B=0x00;
    TCNT2=0x00;
    OCR2A=0x00;
    OCR2B=0x00;

    /*// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// Interrupt on any change on pins PCINT0-7: On
// Interrupt on any change on pins PCINT8-14: On
// Interrupt on any change on pins PCINT16-23: On
EICRA=0x00;
EIMSK=0x00;
PCICR=0x07;
PCMSK0=0x01;
PCMSK1=0x3F;
PCMSK2=0x03;
PCIFR=0x07;*/

    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    // Interrupt on any change on pins PCINT0-7: On
    // Interrupt on any change on pins PCINT8-14: Off
    // Interrupt on any change on pins PCINT16-23: On
    EICRA=0x00;
    EIMSK=0x00;
    PCICR=0x05;
    PCMSK0=0x01;
    PCMSK2=0x03;
    PCIFR=0x05;

    // Timer/Counter 0 Interrupt(s) initialization
    TIMSK0=0x01;

    // Timer/Counter 1 Interrupt(s) initialization
    TIMSK1=0x01;

    // Timer/Counter 2 Interrupt(s) initialization
    TIMSK2=0x00;

    // USART initialization
    // USART disabled
    UCSR0B=0x00;

    // Analog Comparator initialization
    // Analog Comparator: Off
    // Analog Comparator Input Capture by Timer/Counter 1: Off
    ACSR=0x80;
    ADCSRB=0x00;
    DIDR1=0x00;

    // ADC initialization
    // ADC Clock frequency: 500.000 kHz
    // ADC Voltage Reference: AVCC pin
    // ADC Auto Trigger Source: Timer1 Overflow
    // Only the 8 most significant bits of
    // the AD conversion result are used
    // Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
    // ADC4: On, ADC5: On
    DIDR0=0x00;
    ADMUX=ADC_VREF_TYPE & 0xff;
//  ADCSRA=0xA9;
	ADCSRA=0b10000111;
    ADCSRB&=0xF0;
//    ADCSRB|=0x06;

    // SPI initialization
    // SPI Type: Master
    // SPI Clock Rate: 2*2000.000 kHz
    // SPI Clock Phase: Cycle Start
    // SPI Clock Polarity: Low
    // SPI Data Order: MSB First
    SPCR=0x50;
    SPSR=0x01;

    // TWI initialization
    // TWI disabled
    TWCR=0x00;



}
